//
//  SendHttpRequest.m
//  MyCar
//
//  Created by QQ on 15/5/27.
//  Copyright (c) 2015年 QQ365. All rights reserved.
//

#import "SendHttpRequest.h"
#import "AFNetworking.h"
#import "AFHTTPRequestOperationManager.h"
#import "MBProgressHUD+MJ.h"
@implementation SendHttpRequest


+(void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    // 1.获得请求管理者
    AFHTTPRequestOperationManager *mag= [AFHTTPRequestOperationManager manager];
    // 请求的序列化
    mag.requestSerializer = [AFHTTPRequestSerializer serializer];
    //响应的序列化
    mag.responseSerializer=[AFHTTPResponseSerializer serializer];
    //请求可以接受的类型（text／html）
    mag.responseSerializer.acceptableContentTypes=[mag.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    [mag.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    // 2.发送POST请求
    [mag POST:url parameters:params
      success:^(AFHTTPRequestOperation *operation, id responseObj) {
          if (success) {
              success(responseObj);
          }
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          if (failure) {
              failure(error);
          }
      }];
}

+(void)get:(NSString *)url success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    // 1.获得请求管理者
    AFHTTPRequestOperationManager *mag= [AFHTTPRequestOperationManager manager];
    // 请求的序列化
    mag.requestSerializer = [AFHTTPRequestSerializer serializer];
    //响应的序列化
    mag.responseSerializer=[AFHTTPResponseSerializer serializer];
    //请求可以接受的类型（text／html）
    mag.responseSerializer.acceptableContentTypes=[mag.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    [mag.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
//发送get请求
    
    [mag GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

+(void)get:(NSString *)url parameters:(NSDictionary *)dic success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    // 1.获得请求管理者
    AFHTTPRequestOperationManager *mag= [AFHTTPRequestOperationManager manager];
    
    //发送get请求
    
    [mag GET:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}
+(void)put:(NSString *)url parameters:(id)identifier success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    // 1.获得请求管理者
    AFHTTPRequestOperationManager *mag= [AFHTTPRequestOperationManager manager];
    
    //发送get请求
    [mag PUT:url parameters:identifier success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
//    [mag GET:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        if (success) {
//            success(responseObject);
//        }
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        if (failure) {
//            failure(error);
//        }
//    }];
}
@end
